#region Copyright note and modification history

/*
-----------------------------------------------------------------------------
Copyright (c) 2006 HSI Inc. All Rights Reserved.

PROPRIETARY NOTICE: This software has been provided pursuant to a
License Agreement that contains restrictions on its use. This software
contains valuable trade secrets and proprietary information of
HSI Inc and is protected by Federal copyright law. It may not
be copied or distributed in any form or medium, disclosed to third parties,
or used in any manner that is not provided for in said License Agreement,
except with the prior written authorization of HSI Inc.

-----------------------------------------------------------------------------
$Log: $
-----------------------------------------------------------------------------
*/

#endregion

using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace SimplePushpinGenerator
{
    /// <SUMMARY> 
    /// Inherited child for the class Graphics encapsulating 
    /// additional functionality for curves and rounded rectangles. 
    /// </SUMMARY> 
    public static class GraphicsExtensions
    {

        /// <summary>
        ///Fills a Rounded Rectangle with integers. 
        /// 
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="brush"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="radius"></param>
        public static void FillRoundRectangle(this Graphics graphics,
                                       Brush brush,
                                       int x, int y,
                                       int width, int height, int radius)
        {
            float fx = Convert.ToSingle(x);
            float fy = Convert.ToSingle(y);
            float fwidth = Convert.ToSingle(width);
            float fheight = Convert.ToSingle(height);
            float fradius = Convert.ToSingle(radius);
            FillRoundRectangle(graphics,brush, fx, fy,
                               fwidth, fheight, fradius);
        }


        /// <summary>
        /// Fills a Rounded Rectangle with continuous numbers.
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="brush"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="radius"></param>
        public static void FillRoundRectangle(this Graphics graphics,
                                       Brush brush,
                                       float x, float y,
                                       float width, float height, float radius)
        {
            var rectangle = new RectangleF(x, y, width, height);
            GraphicsPath path = rectangle.GetRoundedRect(radius);
            graphics.FillPath(brush, path);
        }


        /// <summary>
        /// Draws a Rounded Rectangle border with integers. 
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="pen"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="radius"></param>
        public static void DrawRoundRectangle(this Graphics graphics,
                                       Pen pen, int x, int y,
                                       int width, int height, int radius)
        {
            float fx = Convert.ToSingle(x);
            float fy = Convert.ToSingle(y);
            float fwidth = Convert.ToSingle(width);
            float fheight = Convert.ToSingle(height);
            float fradius = Convert.ToSingle(radius);
            DrawRoundRectangle(graphics,pen, fx, fy, fwidth, fheight, fradius);
        }


        /// <summary>
        /// Draws a Rounded Rectangle border with continuous numbers. 
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="pen"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="radius"></param>
        public static void DrawRoundRectangle(this Graphics graphics,Pen pen,
                                       float x, float y,
                                       float width, float height, float radius)
        {
            var rectangle = new RectangleF(x, y, width, height);
            GraphicsPath path = rectangle.GetRoundedRect(radius);
            graphics.DrawPath(pen, path);
        }

        /// <summary>
        /// Get the width of a given string and font to use
        /// </summary>
        /// <param name="graphics">Graphics object used for measurement</param>
        /// <param name="text">text string whose width is measured</param>
        /// <param name="font">Font to use</param>
        /// <returns>Width of given string</returns>
        public static int MeasureDisplayStringWidth(this Graphics graphics, string text, Font font)
        {
            var format = new StringFormat();
            var rect = new RectangleF(0, 0, 1000, 1000);
            CharacterRange[] ranges = { new CharacterRange(0, text.Length) };

            format.SetMeasurableCharacterRanges(ranges);
            format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;

            Region[] regions = graphics.MeasureCharacterRanges(text, font, rect, format);
            rect = regions[0].GetBounds(graphics);

            return (int)(rect.Right + 1.0f);
        }



       

    }
}